<template>
  <div class='list'>
    <ul class='name'>
      <li
        v-for='(item,key) of cities'
        :key='key'
        :ref= 'key'
        @click='handleLetterClick'
        @touchstart='handleTouchStart'
        @touchmove='handleMoveStatus'
        @touchend='handleTouchEnd'
      >{{key}}</li>
    </ul>
  </div>

</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  data () {
    return {
      touchstatus: false,
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  computed: {
    letters () {
      const letters = []
      for (var i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerHTML)
      // console.log(e.target.innerHTML)
    },
    handleTouchStart () {
      this.touchstatus = true
    },
    handleMoveStatus (e) {
      if (this.touchstatus) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 80
          const index = Math.floor((touchY - this.startY) / 17)
          if (index > 0 && index <= this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 16)
      }
    },
    handleTouchEnd () {
      this.touchstatus = false
    }
  }
}
</script>

<style lang='stylus' scoped>
  @import '~styles/varibles.styl'
  .list
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    top: 1.6rem
    right:0
    bottom:0
    width: .3rem
    .name
      line-height: .35rem
      color: $bgColor
      margin: 0 auto
</style>
